<?php
/**
 *
 */
class ctrl_message extends ctrl_base {
    private static $dirName = 'message';
    
    /**
     * 新闻列表
     */
    public static function main() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('msg_list'));
    	$currentPage = isset($_POST['pageNum']) ? $_POST['pageNum'] : 1;
    	$pagesize = isset($_POST['numPerPage']) ? $_POST['numPerPage'] : self::$_pagesize;
		$nextpage = ($currentPage - 1) * $pagesize;
    	
    	$keyword = isset($_POST['keyword']) ? urldecode($_POST['keyword']) : '';
		$id = isset($_POST['category_id']) ? intval($_POST['category_id']) : '';
    	$condition = array();
    	if(trim($keyword) != '') {
    		$condition['keyword'] = $keyword;
    	}
		if(!empty($id)){
			$condition['id'] = $id;
		}
    	$msg_list = model_message :: messageList($nextpage,$pagesize,$condition);
		foreach($msg_list as $k=>$v){
			if(isset($v['message'])){
				$msg_list[$k]['message'] = nl2br($v['message']);
			}
		}
    	$total = array_pop($msg_list);
    	$pageShow = framework_static_common::page(self::$_pagesize, $total, $currentPage);
    	
    	self::smarty() -> assign('keyword', $keyword);
    	self::smarty() -> assign('pageShow', $pageShow);
    	self::smarty() -> assign('msg_list', $msg_list);

		//新添加
		$titles = model_message::get_meg_title();
		self::smarty() -> assign('titles', $titles);
		self::smarty() -> assign('total', $total);
		self::smarty() -> assign('pageNum', $currentPage);
		self::smarty() -> assign('numPerPage', $pagesize);
		self::smarty() -> assign('pageNumShown', self::$_pageNumShown);
		self::smarty() -> assign('keyword', $keyword);
		self::smarty() -> assign('message_id', $category_id);
		self::smarty() -> assign('shoNum', self::$_shoNum);

    	self::smarty() -> display(self::$dirName . '/msg_list.html');    	
    }
    
    public static function messageAdd() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('msg_add'));
    	if($_POST){
			$info['rule_name'] = trim($_POST['rule_name']);
			$info['keywords'] = trim($_POST['keywords']);
			$info['message'] = trim($_POST['message']);
			$info['author'] = $_SESSION['admin']['username'];
			$info['editer'] = $_SESSION['admin']['username'];
			$rs = model_message::messageAdd($info);
		    if($rs <= 0){
	    		self::ajax_ouput(300, '操作失败');
	    	} else {
	    		self::ajax_ouput(200, '操作成功','auto_replay','closeCurrent');
	    	} 
    	}
    	self::smarty() -> display(self::$dirName . '/msg_add.html');
    	
    }
    
	/*
	 * 删除文章
	 */
    public static function messageDelete() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('msg_delete'));
    	$id = isset($_POST['ids'])? $_POST['ids']:0;
		$id = explode(',', $id);
		foreach($id as $v){
			$ids[] = intval($v);
		}
    	$data = array('delete_flag' => 1);
    	$st = model_message::updatemessageInfo($ids, $data);
    	if($st > 0) {
			self::ajax_ouput(200, '操作成功');
    	} else {
    		self::ajax_ouput(300, '操作失败');
    	}
    }
	
    public static function messageEdit() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$id = isset($_GET['id'])? intval($_GET['id']):intval($_POST['id']);
    	if($id <= 0) {
			self::ajax_ouput(300, '非法访问');
    	}
		$info = model_message::getmessageInfoById($id);
		$ctrl_tag = 'msg_edit_other';
		if($info['author'] == $_SESSION['admin']['username']){
			$ctrl_tag = 'msg_edit_self';
		}
		self::operation(array($ctrl_tag));
    	if($_POST){
			$info = array();
			$info['rule_name'] = trim($_POST['rule_name']);
			$info['keywords'] = trim($_POST['keywords']);
			$info['message'] = trim($_POST['message']);
			$info['editer'] = $_SESSION['admin']['username'];

			$rs = model_message::updatemessageInfo(explode(',',$id), $info);
	    	
		    if($rs >= 0){
	    		self::ajax_ouput(200, '操作成功','auto_replay','closeCurrent');
	    	} else {
	    		self::ajax_ouput(300, '操作失败');
	    	}    		
    	}
    	self::smarty() -> assign('id', $id);
    	self::smarty() -> assign('info', $info);
    	self::smarty() -> display(self::$dirName . '/msg_edit.html');
    }
	//批量上传模板下载
	public static function get_betch_model(){
		self::download_xls_output(PATH_WWW.DOWNLOAD_FILES_PATH.'betch_add_words'.self::$_excel_pre);
	}
	/*
	* @desc 解析xls
	* @author kevin wang
	* @date 2014-09-19
	* @param void
	**/
	public static function parse_xls(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		//文件上传
		$info = self::file_upload_xls();
		//xls文件解析
		framework_base_autoloader::import_vendor('excel/PHPExcel.php');
		$objPHPExcel = new PHPExcel();
		$objPHPExcel = PHPExcel_IOFactory::load($info['url']);
		$keywords = $objPHPExcel->getSheet(0)->toArray();
		
		//数据读取
		$str = "";
		foreach($keywords as $keys => $values){
			foreach($values as $k => $v){
				$str .= $keywords[$keys][$k]."#";
			}
		}
		$info['text'] = trim($str, "#");
		framework_static_function::output_json($info, false);
	}
    
}
